package linter.linterjava.controller.curriculum;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import linter.linterjava.POJO.DO.Curriculum;
import linter.linterjava.POJO.DO.CurriculumNote;
import linter.linterjava.POJO.DO.CurriculumPlan;
import linter.linterjava.POJO.DTO.CurriculumDTO;
import linter.linterjava.POJO.DTO.CurriculumNoteDTO;
import linter.linterjava.POJO.DTO.CurriculumPlanDTO;
import linter.linterjava.POJO.DTO.UserDTO;
import linter.linterjava.POJO.UtilDTO.PageInfoDTO;
import linter.linterjava.custom.ResultVO;
import linter.linterjava.service.*;
import linter.linterjava.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static linter.linterjava.custom.CustomResultType.USER_NOT_EXIST;

/**
 * @author 北煜
 */
@Slf4j
@Api(tags = "课程基础模块")
@RestController
@RequestMapping("curriculum")
public class BaseCurriculumController {

    @Resource
    UserService userService;
    @Resource
    CurriculumService curriculumService;
    @Resource
    CurriculumPlanService curriculumPlanService;
    @Resource
    CurriculumNoteService curriculumNoteService;
    private final UserQueryService userQueryService;
    @Autowired
    public BaseCurriculumController(UserQueryService userQueryService) { this.userQueryService = userQueryService; }
    @GetMapping("/show/getUserByUsername/{username}")
    @ApiOperation("根据用户名查找用户")
    public ResultVO<UserDTO> getUserByUsername(@PathVariable String username) { return userQueryService.getUserByUsername(username); }

    /**
     * @param curriculumDTO 输入名称/节点/类型
     */
    @PostMapping("queryCurriculum")
    @ApiOperation("搜索课程")
    public ResultVO<List<Curriculum>> queryCurriculum(@RequestBody CurriculumDTO curriculumDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(curriculumService.queryCurriculum(curriculumDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    /**
     * @param curriculumDTO 传入分页属性，课程类型
     */
    @PostMapping("getAllCurriculumByType")
    @ApiOperation("得到所有课程")
    public ResultVO<PageInfo<Curriculum>> getAllCurriculumByType(@RequestBody CurriculumDTO curriculumDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(curriculumService.getAllCurriculumByType(curriculumDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("getCurriculumById")
    @ApiOperation("根据id得到课程")
    public ResultVO<Curriculum> getCurriculumById(@RequestBody CurriculumDTO curriculumDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(curriculumService.getCurriculumById(curriculumDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    /**
     * 创建用户课程进度，更新课程进度
     *
     * @param curriculumPlanDTO 课程进度 分页 类型
     */
    @PostMapping("curriculumPlan")
    @ApiOperation("课程进度")
    public ResultVO<String> curriculumPlan(@RequestBody CurriculumPlanDTO curriculumPlanDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(curriculumPlanService.curriculumPlan(curriculumPlanDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("getCurriculumPlan")
    @ApiOperation("获取用户课程进度")
    public ResultVO<Integer> getCurriculumPlan(@RequestBody CurriculumPlanDTO curriculumPlanDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(curriculumPlanService.getCurriculumPlan(curriculumPlanDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("getLearningCurriculum")
    @ApiOperation("获取正在学习中的课程")
    public ResultVO<PageInfo<Curriculum>> getLearningCurriculum(@RequestBody CurriculumPlanDTO curriculumPlanDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(curriculumPlanService.getLearningCurriculum(curriculumPlanDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("getCurriculumBase")
    @ApiOperation("获取课程库")
    public ResultVO<PageInfo<Curriculum>> getCurriculumBase(@RequestBody PageInfoDTO pageInfoDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(curriculumService.getCurriculumBase(pageInfoDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("getCurriculumFinish")
    @ApiOperation("获取已完成的课程")
    public ResultVO<PageInfo<Curriculum>> getCurriculumFinish(@RequestBody CurriculumPlanDTO curriculumPlanDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(curriculumPlanService.getCurriculumFinish(curriculumPlanDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    /**
     * @param curriculumNoteDTO 需要课程id 类型
     */
    @PostMapping("createCurriculumNote")
    @ApiOperation("创建学习笔记")
    public ResultVO<?> createCurriculumNote(@RequestBody CurriculumNoteDTO curriculumNoteDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(curriculumNoteService.createCurriculumNote(curriculumNoteDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    /**
     * @param curriculumNoteDTO 需要id
     */
    @PostMapping("getCurriculumNoteById")
    @ApiOperation("根据学习笔记id获取学习笔记")
    public ResultVO<CurriculumNote> getCurriculumNoteById(@RequestBody CurriculumNoteDTO curriculumNoteDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(curriculumNoteService.getCurriculumNoteById(curriculumNoteDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("getCurriculumNoteList")
    @ApiOperation("获取课程笔记列表")
    public ResultVO<List<CurriculumNote>> getCurriculumNoteList() {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(curriculumNoteService.getCurriculumNoteList(uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("queryCurriculumNote")
    @ApiOperation("搜索课程笔记")
    public ResultVO<List<CurriculumNote>> queryCurriculumNote(@RequestBody CurriculumNoteDTO curriculumNoteDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(curriculumNoteService.queryCurriculumNote(curriculumNoteDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

}
